import cloudbase, { CloudBase, ICallFunctionOptions } from "@cloudbase/node-sdk";
import { secretId, secretKey } from "../const";

let tcbIns: TCB;

/**
 * 匿名登录
 * @return {Promise<void>}
 */
class TCB {
  // 环境ID
  private envId = process.env.CLOUDBASE_ENV_ID as string || 'cloudbase-prepaid-8e7eb446cdbcb5';
  app = {} as CloudBase;
  auth = {} as IAnyObject;

  // 初始化app
  initApp() {
    this.app = cloudbase.init({
      env: 'cloudbase-prepaid-8e7eb446cdbcb5',
      region: "ap-shanghai",
      secretId, 
      secretKey
    });
    this.initAuth();
  }
  
  // 鉴权
  initAuth() {
    this.auth = this.app.auth();
  }

  // 函数调用
  callFunction (name = '', data = {}): Promise<IAnyObject> {
    // 函数调用
    return this.app.callFunction({
      name,
      data,
    });
  }

  // 存储调用
}

export const getTCBInstance = () => {
  if (tcbIns) {
    return tcbIns;
  }

  tcbIns = new TCB();

  return tcbIns;
}